﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ServiceModel;
using System.Text;
using Bookvar.ApplicationModel;
using Bookvar.Collaboration;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions.Collaboration;
using Bookvar.UI;
using Bookvar.Collaboration.ClientSide;
using System.Diagnostics;

namespace BookvarWpf
{
    public class MainPagePresenter : IMainPagePresenter
    {
        private IMainPage mainPage;
        
        private readonly ICollaborationManager collaborationManager;

        public MainPagePresenter(IMainPage mainPage)
        {
            if(mainPage == null)
            {
                throw new ArgumentNullException("mainPage");
            }

            this.mainPage = mainPage;
            collaborationManager = BookvarApplication.Current.ServiceLayer.GetService<ICollaborationManager>();
            Debug.Assert(collaborationManager != null);

            collaborationManager.CollaborationStopped += CollaborationManager_CollaborationStopped;
            collaborationManager.ServerStarted += CollaborationManager_ServerStarted;
            collaborationManager.ConnectionToServerLost += CollaborationManager_ConnectionToServerLost;
            collaborationManager.ServerStopped += CollaborationManager_ServerStopped;
        }

        private void CollaborationManager_ServerStarted(object sender, EventArgs e)
        {
            mainPage.CollaborationServerStarted();
        }

        private void CollaborationManager_CollaborationStopped(object sender, EventArgs e)
        {
            mainPage.CollaborationStopped();
        }

        private void CollaborationManager_ConnectionToServerLost(object sender, EventArgs e)
        {
            mainPage.ShowConnectionToServerLostMessage();
        }

        private void CollaborationManager_ServerStopped(object sender, EventArgs e)
        {
            mainPage.ShowServerStoppedMessage();
        }

        public CollaborationMode CollaborationMode
        {
            get { return collaborationManager.CollaborationMode; }
        }

        /// <summary>
        /// Starts the collaboration server.
        /// </summary>
        /// <param name="mindmap">The mind map.</param>
        /// <param name="hostUserName">Name of the user.</param>
        public void StartCollaborationServer(MindMapPanel mindmap, string hostUserName)
        {
            if(string.IsNullOrEmpty(hostUserName))
            {
                throw new ArgumentException("hostUserName is null or empty.", "hostUserName");
            }

            collaborationManager.StartServer(hostUserName, mindmap.Mindmap);
        }

        public void StopCollaborationServer()
        {
            collaborationManager.StopCollaboration();
            Logger.Log("Server stopped.");
        }

        public IAsyncResult BeginConnectToServer(string host, string clientName, AsyncCallback callback)
        {
            return collaborationManager.BeginConnectToServer(host, clientName, callback);
        }

        public CollaborationUser[] EndConnectToServer(IAsyncResult asyncResult)
        {
            return collaborationManager.EndConnectToServer(asyncResult);
        }

        public void StopCollaborationClient()
        {
            collaborationManager.StopCollaboration();
        }

        public MindMap GetMindmapFromServer()
        {
            MindMap result = collaborationManager.GetMindmap();
            return result;
        }

        public void SendActionToServer(ActionBase action)
        {
            collaborationManager.SendAction(action);
        }
    }
}
